package com.gewu.teday.adapter

import android.annotation.SuppressLint
import android.os.Build
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.RecyclerView
import com.gewu.teday.R
import com.gewu.teday.fragmnet.ZhengTiFragment
import com.gewu.teday.utils.Util

class MoenyListAdapter constructor( private var onclick: Onclick) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {


    private var listItem: java.util.ArrayList<ItemBenan>? = null

    init {
        listItem = ArrayList<ItemBenan>()
    }

    fun addListItem(itemBenan: ItemBenan){
            listItem?.add(itemBenan)
    }

    fun clearListItem(){
        if (!listItem.isNullOrEmpty()){
            listItem?.clear()
        }
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflate: View? = LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false)
        return Hoder(inflate)
    }

    interface Onclick{
      fun  showDialog( itemBenan:ItemBenan,index:Int )
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    @SuppressLint("UseCompatLoadingForDrawables")
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val hoder = holder as Hoder
        when (position) {
            0 -> {
                hoder.time?.text = "日期"
                hoder.count?.text = "总计"
                hoder.shouyi?.text = "创作收益"
                hoder.other?.text = "其他收益"
                hoder.image?.visibility = View.VISIBLE
                hoder.image2?.visibility = View.GONE
            }
            1 -> {
                hoder.image?.visibility = View.GONE
                hoder.image2?.visibility = View.VISIBLE
                if (Util.afterTimeCalendar()){
                    hoder.time?.text = listItem?.get(position-1)?.time
                    hoder.count?.text = listItem?.get(position-1)?.count
                    hoder.shouyi?.text = listItem?.get(position-1)?.count
                    hoder.other?.text = 0.toString()
                }else{
                    hoder.time?.text = listItem?.get(position-1)?.time
                    hoder.count?.text = 0.toString()
                    hoder.shouyi?.text = "待更新"
                    hoder.other?.text = 0.toString()
                }
                hoder.show_money?.setOnClickListener {
                    listItem?.let { it1 -> onclick.showDialog(it1[position-1],position-1) }
                }
            }
            else -> {
                hoder.image?.visibility = View.GONE
                hoder.image2?.visibility = View.VISIBLE
                hoder.time?.text = listItem?.get(position-1)?.time
                hoder.count?.text = listItem?.get(position-1)?.count
                hoder.shouyi?.text = listItem?.get(position-1)?.count
                hoder.other?.text = 0.toString()
                hoder.show_money?.setOnClickListener {
                    listItem?.let { it1 -> onclick.showDialog(it1[position-1],position-1) }
                }
            }
        }
    }

    override fun getItemCount(): Int =if (listItem.isNullOrEmpty()) 0 else  listItem!!.size+1

    class Hoder(inflate: View?) : RecyclerView.ViewHolder(inflate!!) {
        val time: TextView? = inflate?.findViewById<TextView>(R.id.time)
        var count: TextView? = inflate?.findViewById<TextView>(R.id.count)
        var shouyi: TextView? = inflate?.findViewById<TextView>(R.id.shouyi)
        var image: ImageView? = inflate?.findViewById<ImageView>(R.id.image)
        var image2: ImageView? = inflate?.findViewById<ImageView>(R.id.image2)
        var other: TextView? = inflate?.findViewById<TextView>(R.id.other)
        var show_money: RelativeLayout? = inflate?.findViewById<RelativeLayout>(R.id.show_money)

    }

}

data class ItemBenan(var time: String, var count: String)
